package com.jtpay.ysfproductionprocess.utils;

import java.nio.charset.Charset;

public class StringUtil {
    public static String ToHex(String s,boolean fenge)
    {
        if ((s.length() % 2) != 0)
        {
            s += " ";//空格
        }
        byte[] bytes = s.getBytes(Charset.defaultCharset());
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < bytes.length; i++)
        {
            str.append(String.format("%x", bytes[i]));
            if (fenge && (i != bytes.length - 1))
            {
                str.append(String.format("%s", ","));
            }
        }
        return str.toString().toLowerCase();
    }

    public static String byteArrayToStr(byte[] byteArray) {
        if (byteArray == null) {
            return null;
        }
        return new String(byteArray);
    }
    public static String hexStrToStr(String hexStr) {
        String str = "0123456789ABCDEF";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        return new String(bytes);
    }
    public static String strTo16(String s) {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            int ch = (int) s.charAt(i);
            String s4 = Integer.toHexString(ch);
            str.append(s4);
        }
        return str.toString();
    }
    public static String bytesToHexString(byte[] src){
        int length = 0;
        for (int i = 0; i < src.length; ++i) {
            if (src[i] != 0) {
                length = i + 1;
            }
        }

        StringBuilder stringBuilder = new StringBuilder("");
        if (src.length <= 0) {
            return null;
        }
        for (int i = 0; i < length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
    public static byte[] strToByteArray(String str) {
        if (str == null) {
            return null;
        }
        return str.getBytes();
    }
}
